package com.dycc.libary.router_interceptor;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;

/**
 * 拦截器基类
 * Created by wxk on 2018/1/21 0021.
 */

public abstract class BaseInterceptor implements IInterceptor {
    private InterceptorScope scope;
    private InterceptorBind binds;
    public static final String BIND_KEY="bindkey";
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        this.scope=initScope();
        try {
            binds = (InterceptorBind) postcard.getExtras().getSerializable(BIND_KEY);
        } catch (Exception ex) {
        }

        switch (scope) {
            case GLOBAL://全局
                if (binds != null && binds.checkBind(this.getClass()) != null
                        && !binds.checkBind(this.getClass()).isEnable()) {
                    callback.onContinue(postcard);
                    return;
                }
                break;
            case SPECIFIED://指定
                if (binds == null || binds.checkBind(this.getClass()) == null
                        || !binds.checkBind(this.getClass()).isEnable()) {
                        callback.onContinue(postcard);
                    return;
                }
                break;
        }
        handle(postcard, callback);
    }

    /**
     * 初始化作用域
     *
     * @return
     */
    protected abstract InterceptorScope initScope();


    @Override
    public void init(Context context) {

    }

    public abstract void handle(Postcard postcard, InterceptorCallback callback);

    public enum InterceptorScope {
        GLOBAL,
        SPECIFIED
    }

}
